﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("[START PROGRAM]");

            //TestGet1();
            //TestGet2();

            TestPost();

            Console.WriteLine("[END PROGRAM]");
            Console.ReadLine();
        }

        public class credentials
        {
            public string userName;
            public string password;
        }
        public static async void TestPost()
        {
            string result = string.Empty;
            System.Web.Script.Serialization.JavaScriptSerializer javascriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            using (System.Net.Http.HttpClient clt = new System.Net.Http.HttpClient())
            {
                clt.BaseAddress = new Uri("http://dev.mvc.cocadre.admin.api/");
                clt.DefaultRequestHeaders.Accept.Clear();
                clt.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //clt.DefaultRequestHeaders.Add("Content-Type", "application/json");

                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                list.Add(new KeyValuePair<string, string>("userName", "zhixian"));
                list.Add(new KeyValuePair<string, string>("password", "zpasspwd"));

                dynamic expando = new System.Dynamic.ExpandoObject();
                expando.userName = "Brian";
                expando.password = "USA";

                //credentials c = new credentials();
                //c.userName = "zhixian";
                //c.password = "zpaspwd";
                

                //System.Net.Http.FormUrlEncodedContent httpContent = new FormUrlEncodedContent(list);
                System.Net.Http.StringContent httpContent = new StringContent(javascriptSerializer.Serialize(expando), Encoding.UTF8, "application/json");

                HttpResponseMessage response = await clt.PostAsync("api/echo", httpContent);

                // this works
                //HttpResponseMessage response = await clt.PostAsJsonAsync<credentials>("api/echo", c);

                if (response.IsSuccessStatusCode)
                {

                    if (response.Content.Headers.ContentType.MediaType == "application/json")
                    {
                        result = await response.Content.ReadAsStringAsync();
                    }

                    string x = javascriptSerializer.Deserialize<string>(result);

                    Console.WriteLine("SUCCESS");

                }
                else
                {
                    Console.WriteLine("FAILURE");
                }

            }

        } // public void TestGet()


        public static async void TestGet2()
        {
            string result = string.Empty;
            System.Web.Script.Serialization.JavaScriptSerializer javascriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            using (System.Net.Http.HttpClient clt = new System.Net.Http.HttpClient())
            {
                clt.BaseAddress = new Uri("http://dev.mvc.cocadre.admin.api/");
                clt.DefaultRequestHeaders.Accept.Clear();
                clt.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // New code:
                HttpResponseMessage response = await clt.GetAsync("api/echo?message=mymsg");

                if (response.IsSuccessStatusCode)
                {

                    if (response.Content.Headers.ContentType.MediaType == "application/json")
                    {
                        result = await response.Content.ReadAsStringAsync();
                    }

                    string x = javascriptSerializer.Deserialize<string>(result);

                    Console.WriteLine("SUCCESS");

                }
                else
                {
                    Console.WriteLine("FAILURE");
                }

            }

        } // public void TestGet()

        public static async void TestGet1()
        {
            string result = string.Empty;
            System.Web.Script.Serialization.JavaScriptSerializer javascriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            using (System.Net.Http.HttpClient clt = new System.Net.Http.HttpClient())
            {
                clt.BaseAddress = new Uri("http://dev.mvc.cocadre.admin.api/");
                clt.DefaultRequestHeaders.Accept.Clear();
                clt.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // New code:
                HttpResponseMessage response = await clt.GetAsync("api/echo");

                if (response.IsSuccessStatusCode)
                {
                    
                    if (response.Content.Headers.ContentType.MediaType == "application/json")
                    {
                        result = await response.Content.ReadAsStringAsync();
                    }

                    List<string> x = javascriptSerializer.Deserialize<List<string>>(result);

                    Console.WriteLine("SUCCESS");

                }
                else
                {
                    Console.WriteLine("FAILURE");
                }

            }

        } // public void TestGet()

    }
}
